package com.android.vmg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


public class Score extends Activity {
	
	private final int GAME_ACT = 1;

	private TextView scoreText;
	private int numCircles;
	private Button buttonNext;
	private int points;
	
	@Override
	public void onCreate(Bundle savedState) {
		super.onCreate(savedState);
		points = 0;
		numCircles = 3;
		setContentView(R.layout.score);
    	scoreText = ((TextView)findViewById(R.id.textView1));
    	buttonNext = (Button)findViewById(R.id.button_next);
    	callGame();
	}
	
	private void callGame() {
    	Intent intent = new Intent(this, Game.class);
    	Bundle b = new Bundle();
    	b.putInt("numCircles", numCircles);
    	intent.putExtras(b);
    	startActivityForResult(intent, GAME_ACT);
	}
	
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	try {
	    	if(requestCode == GAME_ACT) {
	    		if(resultCode == RESULT_OK) {
	    			String txt;
	    			points += data.getIntExtra("points", 0);
	    			switch(data.getIntExtra("state", -1)) {
	    			case GameThread.WIN:
	    				txt = "Partida ganha! Pontuacao: " + Integer.valueOf(points).toString();
	                    scoreText.setText(txt);
	    				buttonNext.setVisibility(View.VISIBLE);
	    				break;
	    			case GameThread.LOSE:
	    				txt = "Fim de jogo! Pontuacao: " + Integer.valueOf(points).toString();
	    				scoreText.setText(txt);
	    				buttonNext.setVisibility(View.GONE);
	    				break;
	    			}
	    		}
	    	}
    	} catch (Exception e) {
    		Log.w("", e.getMessage());
    	}
    } 
	
    public void next(View v) {
    	numCircles ++;
    	callGame();
    }
    
    private void refreshRanking() {
    	AppPreferences appPrefs = new AppPreferences(getApplicationContext());
    	appPrefs.saveScore(points);
    }
    
	public void back(View v) {
		refreshRanking();
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		finish();
	}
}
